package com.example.gokuplayalong.bases.traces;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import androidx.annotation.NonNull;

import com.example.gokuplayalong.bases.beans.AppParams;
import com.example.gokuplayalong.bases.exception.ExceptionUtils;
import com.example.gokuplayalong.bases.network.updown.Upload;
import com.example.gokuplayalong.bases.tools.Logger;
import com.example.gokuplayalong.bases.tools.Tools;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

import okhttp3.MediaType;

@Aspect
public class TraceAspect {


    @Before("execution(* com.example.gokuplayalong.MainActivity.onStart())")
    public void MainActivityOnStart() {
        UserTraces.writeTraces("---START------ 进入 MainActivity ------START---");
    }

    @Before("execution(* com.example.gokuplayalong.MainActivity.onPause())")
    public void MainActivityOnPause() {
        try {
            UserTraces.writeTraces("---PAUSE------ 离开 MainActivity ------PAUSE---");

            Upload.getInstance()
                    .uploadFile(MediaType.parse("text/plain"), "userTrace.txt", AppParams.getInstance().appCurrentUser.getJsonObject()
                            .getString("phone") + "_" + Tools.getUUID() + "_trace.txt", new Handler(Looper.getMainLooper()) {
                        @Override
                        public void handleMessage(@NonNull Message msg) {
                            super.handleMessage(msg);
                            switch (msg.what) {
                                case 1: {
                                    Logger.d("上传操作日志成功");
                                }
                                break;
                                case 2: {
                                    Logger.d("上传操作日志失败");
                                }
                                ;
                            }
                        }
                    });


        } catch (Throwable e) {
            ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
        }
    }

    @Before("execution(* com.example.gokuplayalong.service.fragments.main.UserMainReceive.onResume())")
    public void UserMainReceiveOnResume() {
        UserTraces.writeTraces("进入 UserMainReceive");
    }

    @Before("execution(* com.example.gokuplayalong.service.fragments.main.UserMainPublish.onResume())")
    public void UserMainPublishOnResume() {
        UserTraces.writeTraces("进入 UserMainPublish");
    }

    @Before("execution(* com.example.gokuplayalong.service.fragments.main.UserMainMessage.onResume())")
    public void UserMainMessageOnResume() {
        UserTraces.writeTraces("进入 UserMainMessage");
    }

    @Before("execution(* com.example.gokuplayalong.service.fragments.main.UserMainProfile.onResume())")
    public void UserMainProfileOnResume() {
        UserTraces.writeTraces("进入 UserMainProfile");
    }

    @Before("execution(* com.example.gokuplayalong.service.fragments.user.receive.common.UserMainReceiveList.initChanged())")
    public void UserMainReceiveListInitChanged() {
        UserTraces.writeTraces("进入 UserMainReceiveList");
    }

    @Before("execution(* com.example.gokuplayalong.service.fragments.user.publish.gloryking.UserMainPublishGloryKing.onResume())")
    public void UserMainPublishGloryKingOnResume() {
        UserTraces.writeTraces("进入 UserMainPublishGloryKing");
    }

    @Before("execution(* com.example.gokuplayalong.service.fragments.user.publish.gloryking.UserMainPublishGloryKingInvoices.initChanged())")
    public void UserMainPublishGloryKingInvoicesInitChanged() {
        UserTraces.writeTraces("进入 UserMainPublishGloryKingInvoices");
    }

    @Before("execution(* com.example.gokuplayalong.service.fragments.user.receive.gloryking.UserMainReceiveGloryKingFlow.initChanged())")
    public void UserMainReceiveGloryKingFlowInitChanged() {
        UserTraces.writeTraces("进入 UserMainReceiveGloryKingFlow");
    }

    @Before("execution(* com.example.gokuplayalong.service.fragments.user.profile.UserMainProfileNewUserTask.initChanged())")
    public void UserMainProfileNewUserTaskInitChanged() {
        UserTraces.writeTraces("进入 UserMainProfileNewUserTask");
    }


    @Before("execution(* com.example.gokuplayalong.service.fragments.user.profile.UserMainProfileOnlineCustom.initChanged())")
    public void UserMainProfileOnlineCustomTaskInitChanged() {
        UserTraces.writeTraces("进入 UserMainProfileOnlineCustom");
    }
}
